<script setup>
  import { ref, onMounted } from 'vue'
  import { getAddressListApi } from "@/api";
  import {message} from "ant-design-vue";
  const emit = defineEmits(['selected'])
  const visible = ref(false)
  const addressList = ref([])
  const getAddressList = () => {
    getAddressListApi().then(res => {
      console.log(res)
      addressList.value = res.data
      console.log(addressList.value)
    }).catch(err => { message.error(err.msg)})
  }
  const open = () => {
    visible.value = true
  }
  const close = () => {
    visible.value = false
  }
  defineExpose({
    open,
    close
  })
  onMounted(() => {
    getAddressList()
  })
</script>

<template>
  <a-modal v-model:open="visible" title="选择地址" :footer="null">
    <div class="address-list">
      <div class="address-item" v-for="(item, index) in addressList" :key="item.id">
        <div class="address-info">
          <div class="name-mobile"> {{ item.name }} {{ item.phone }} </div>
          <div class="address"> {{ item?.modify_region?.name }} {{ item.addr }} </div>
        </div>
        <div class="address-footer">
          <div class="btn" @click="emit('selected', item)"> 选择 </div>
        </div>
      </div>
    </div>
  </a-modal>
</template>

<style scoped lang="scss">
.address-list {
  gap: 20px;
  display: flex;
  flex-direction: column;
  padding: 10px 12px;
  .address-item {
    gap: 20px;
    width: 100%;
    display: flex;
    flex-direction: column;
    padding: 10px;
    margin-bottom: 10px;
    border-radius: 10px;
    box-shadow: rgba(99, 99, 99, 0.2) 0 2px 8px 0;
    .address-info {
      gap: 10px;
      display: flex;
      flex-direction: column;
      .address {
        color: #999;
      }
    }
    .address-footer {
      display: flex;
      align-items: center;
      justify-content: end;
      border-top: 1px solid #eee;
      padding-top: 10px;
      .btn {
        font-size: 12px;
        padding: 4px 20px;
        border: 1px solid #eee;
        border-radius: 20px;
        cursor: pointer;
        transition: 0.6s all;
        &:hover {
          color: var(--primary);
          border-color: var(--primary);
        }
      }
    }
  }
}
</style>